package app.engine.component;

import app.game.ChessPoint;
import app.game.enums.Chess;
import app.game.enums.Direction;

import java.util.*;
import java.util.function.Function;

public class FixedPatternMatcher {

    private Map<Integer, Function<PatternParam, ChessPoint>> patternMap;

    private class PatternParam {
        public Chess[][] chessBoard;
        public List<ChessPoint> chessPoints;
    }

    public FixedPatternMatcher() {
        init();
    }

    public void init() {
        patternMap = new HashMap<>();
        
        patternMap.put(2, FixedPatternMatcher::patternTwo);

    }

    public ChessPoint match(List<ChessPoint> chessPoints, Chess[][] chessBoard, int step) {
        PatternParam param = new PatternParam();
        param.chessBoard = chessBoard;
        param.chessPoints = chessPoints;
        Function<PatternParam, ChessPoint> func = patternMap.get(step);
        if (func == null) {
            return null;
        }
        return func.apply(param);
    }

    /**
     * 第二步定式，
     */
    private static List<Direction> towDirections = Arrays.asList(Direction.UPRIGHT, Direction.RIGHT_OBLIQUE);
    private static ChessPoint patternTwo(PatternParam param) {
        ChessPoint fcp = param.chessPoints.get(0);
        Direction d = towDirections.get(new Random().nextInt(towDirections.size()));
        ChessPoint next = fcp.getNext(d, -1);
        if (next.isOnTable()) {
            return next;
        }

        return fcp.getNext(d, 1);
    }
}
